package com.myapplication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.myapplication.model.SortGoods;
import com.myapplication.zhou.alldemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/30.
 */
public class SortPageRecycleViewAdapter extends RecyclerView.Adapter<SortPageRecycleViewAdapter.MyViewHolder>{
    private List<SortGoods> sortGoodsList = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;
    private int index;
    private MyViewHolderOnClickListener onClickListener =null;

    public SortPageRecycleViewAdapter(Context context, List<SortGoods> sortGoodsList) {
        this.context = context;
        this.sortGoodsList = sortGoodsList;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.sort_page_recycle_view, parent, false);
        MyViewHolder myViewHoler = new MyViewHolder(view,onClickListener);
        return myViewHoler;
    }
    public void setOnItemClickListener(MyViewHolderOnClickListener listener){
        this.onClickListener = listener;
    }
    public void update(List<SortGoods> list) {
        this.sortGoodsList = list;
        notifyDataSetChanged();
    }

    public void changeData(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView_title.setText(sortGoodsList.get(position).getGc_name());
        holder.bindView(sortGoodsList.get(position));
        if (position==index){
            holder.layout.setBackgroundColor(context.getResources().getColor(R.color.toolbar_color));
            holder.textView_title.setTextColor(context.getResources().getColor(R.color.color_red_accent));
        }else{
            holder.layout.setBackgroundColor(context.getResources().getColor(R.color.windowbackground));
            holder.textView_title.setTextColor(context.getResources().getColor(R.color.color_dialog_black));
        }
    }


    @Override
    public int getItemCount() {
        return sortGoodsList.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private MyViewHolderOnClickListener onClickListener;
        private TextView textView_title;
        private LinearLayout layout;

        public MyViewHolder(View itemView,MyViewHolderOnClickListener onClickListener) {
            super(itemView);
            textView_title = (TextView) itemView.findViewById(R.id.textview_title);
            layout = (LinearLayout) itemView.findViewById(R.id.layout);
            layout.setBackgroundColor(context.getResources().getColor(R.color.background));
            this.onClickListener = onClickListener;
            layout.setOnClickListener(this);
        }

        public void bindView(SortGoods sortGoods) {
            layout.setTag(sortGoods);
        }


        @Override
        public void onClick(View v) {
            if(onClickListener!=null){
                onClickListener.onClick(v, (SortGoods) v.getTag(),getLayoutPosition());
            }
        }
    }

    public interface MyViewHolderOnClickListener {
        void onClick(View view, SortGoods sortGoods, int position);
    }
}
